package com.scau.service.Impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.scau.dao.CommentDao;
import com.scau.pojo.Messagecomment;
import com.scau.service.CommentService;
@Service
public class CommentServiceImpl implements CommentService{
	private static Logger logger= LoggerFactory.getLogger(CommentServiceImpl.class);
	
	@Autowired
	CommentDao commentDAO;

	@Override
	public List<Messagecomment> query(int msId) {
		return commentDAO.query(msId);
	}

	@Override
	public int update(JSONObject jsonObject) {
		int result=0;
		Messagecomment comment=new Messagecomment();
		int commentId=jsonObject.getIntValue("id");
		String content=jsonObject.getString("content");
		comment.setCommentId(commentId);
		comment.setCommentContent(content);
		try {
			result=commentDAO.updateCommentById(comment);
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return result;
	}

	@Override
	public int deleteComment(int commentId) {
		return commentDAO.delComment(commentId);
	}
	
	

	@Override
	public int deleteCommentByMsId(int msId) {
		return commentDAO.delCommentByMsId(msId);
	}

	@Override
	public int addComment(JSONObject jsonObject) {
		int result=0;
		Messagecomment comment = new Messagecomment();
		int msId=jsonObject.getIntValue("msId");
		String content=jsonObject.getString("content");
		int userId=jsonObject.getIntValue("userId");
		int replyId=jsonObject.getIntValue("replyId");
		String replyName=jsonObject.getString("replyName");
		String replyNickname=jsonObject.getString("replyNickname");
		String userType=jsonObject.getString("userType");
		comment.setMsId(msId);
		comment.setCommentContent(content);
		comment.setCommentUserId(userId);
		comment.setCommentUserType(userType);
		comment.setCommentReleasetime(new Date());
		comment.setCommentModifytime(new Date());
		if(replyId!=0){
			comment.setReplyId(replyId);
			comment.setReplyName(replyName);
			comment.setReplyNickname(replyNickname);
		}
//		logger.info("----comment------:{}", comment.getReplyId());
		try {
			result=commentDAO.addComment(comment);
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return result;
	}

	@Override
	public int deleteComments(JSONArray jsonArray) {
		int result=0;
		List<Integer> idArr=JSON.parseArray(jsonArray.toString(), Integer.class);
		try {
			for(int id : idArr){
				if(commentDAO.delComment(id)!=1){
					break;
					
				}else{
					result=1;
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return result;
	}

}
